/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.User.Login;



import com.myapp.Metodos.EncripPassword;
import com.myapp.model.usuarios.Usuarios;
import com.myapp.svc.User.UserSvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Jaime
 */
public class UserLoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            ActionForward res=null;
            //Instanciacion de la clase UserLoginSvc de servicio para llamar a la funcion search
            UserSvc userSvc=new UserSvc();
            //
            Usuarios usr=new Usuarios();
            //Ejemplo de recuperacion de datos de un Action Form.
            EncripPassword encPass=new EncripPassword(); 
        usr.setAlias(((UserLoginForm)form).getLogin());
        usr.setPassword(((UserLoginForm)form).getPassword());
        String passwordencriptada=encPass.encripPassword(usr.getPassword());
        usr.setPassword(passwordencriptada);
        usr=userSvc.login(usr);
        if (usr!=null){
            /**
             * El metodo conectar cambia el estado del usuario una vez que se loguea. el estado pasa de desconectado a activo.
             */
            usr=userSvc.conectar(usr);
            HttpSession session=request.getSession();
            session.setAttribute("usuario", usr);
            res=mapping.findForward(SUCCESS);
        }else{
            res=mapping.findForward(ERROR);
        }
        
        return res;
    }
}
